<template>
  <div class="container-fluid">
    <validator name='v'>
      <form novalidate class="form-horizontal">
        <caption><h1 align="center">组织信息</h1></caption>
        <div class="form-group">
          <label for="f_username" class="col-sm-1 control-label col-sm-offset-1 ">上一级：</label>
          <div class="col-sm-2">
            <div class="form-group">
              <input type="text"  v-model="parent.name" class="form-control" id="f_username" placeholder="上一级" disabled>
            </div>
          </div>

          <label for="number" class="col-sm-1 control-label">编号：</label>
          <div class="col-sm-2">
            <div class="form-group">
              <input type="text" v-model="model.number" class="form-control" id="number" placeholder="编号">
            </div>
          </div>

          <label for="name" class="col-sm-1 control-label">组织名称：</label>
          <div class="col-sm-2">
            <div class="form-group">
              <div class="has-feedback"
              :class="{'has-warning':$v.name.required,'has-success': !$v.name.required}">
                <input type="text" v-model="model.name" class="form-control" id="user"
                v-validate:name="{ required: true}"
                placeholder="组织名称" title="组织名称不能为空">
                <span class="glyphicon glyphicon-remove form-control-feedback" v-if="$v.name.required"></span>
                <span class="glyphicon glyphicon-ok form-control-feedback" v-if="!$v.name.required"></span>
              </div>
            </div>
          </div>
        </div>

        <div class="form-group">
          <label for="name" class="col-sm-1 control-label col-sm-offset-1 ">组织性质：</label>
          <div class="col-sm-2">
            <div class="form-group">
              <div class="has-feedback"
              :class="{'has-warning':$v.f_companylevel.required,'has-success': !$v.f_companylevel.required}">
                <input type="text" v-model="model.f_companylevel" class="form-control" id="user"
                v-validate:f_companylevel="{ required: true}"
                placeholder="组织性质" title="组织性质不能为空">
                <span class="glyphicon glyphicon-remove form-control-feedback" v-if="$v.f_companylevel.required"></span>
                <span class="glyphicon glyphicon-ok form-control-feedback" v-if="!$v.f_companylevel.required"></span>
              </div>
            </div>
          </div>
        </div>

        <div class="form-group">
          <label for="name" class="col-sm-1 control-label col-sm-offset-1 ">组件目录：</label>
          <div class="col-sm-2">
            <div class="form-group">
              <div class="has-feedback">
                <input type="text" v-model="model.f_dir" class="form-control" id="user">
              </div>
            </div>
          </div>
        </div>

        <div class="form-group">
          <label for="description" class="col-sm-1 control-label col-sm-offset-1">描述：</label>
          <div class="col-sm-7">
            <div class="form-group">
              <textarea class="form-control" rows="3" style="width: 100%;" v-model="model.f_description"></textarea>
            </div>
          </div>
        </div>
      </form>
      <!-- <button v-if='$v.valid' @click="confirm()" class="btn btn-success col-sm-1 col-sm-offset-2">保&nbsp;&nbsp;&nbsp;存</button> -->
      <button @click="confirm()" class="btn btn-success col-sm-1 col-sm-offset-2" :disabled="!$v.valid">
        <span class="glyphicon glyphicon-floppy-disk"></span>保存
      </button>
      <button @click='$back()' class="btn btn-default col-sm-1 col-sm-offset-1">
        <span class=" glyphicon glyphicon-log-out"></span>返回
      </button>
    </validator>
  </div>
</template>

<script>
import co from 'co'

let saveGen = function * (self) {
  //let msg = yield self.$post('rs/data/save', {data:self.model},{resolveMsg:null, rejectMsg:null})
  console.log(self.model)
  let msg = yield self.$http.post('rs/data/save',{data:self.model})
  yield self.$showMessage(msg.bodyText)
  console.log(msg.bodyText)
  self.$back('refresh')
}

export default {
  title: '项目编写',
  props: ['parent', 'data'],
  ready() {
    this.model.parentid = this.parent.id
    this.model = Object.assign({}, this.model, this.data)
  },
  data () {
    return {
      model: {
        number: '',
        name: '',
        f_description: '',
        selfid: '',
        id: '',
        f_companylevel: '',
        resourcetype: 'organization',
        parentid: ''
      }
    }
  },
  methods: {
    confirm () {
      if (this.model.name === '') {
        this.$showMessage("组织名称不能为空")
        return
      }else if (this.model.f_companylevel === '') {
        this.$showMessage("组织性质不能为空")
        return
      }
      let gen = saveGen(this)
      return co(gen)
    }
  }
}

</script>
